package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.Equipment;
import cn.edu.ujn.park.dao.FeedBack;
import cn.edu.ujn.park.dao.MaintenanceRecord;
import cn.edu.ujn.park.service.IEquipmentService;
import cn.edu.ujn.park.service.IFeedBackService;
import cn.edu.ujn.park.service.IMaintenanceRecordService;
import cn.edu.ujn.park.service.IUserService;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class EquipmentController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IFeedBackService feedBackService;
    @Autowired
    private IMaintenanceRecordService maintenanceRecordService;

    @GetMapping("/userequip")
    public String listu(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "row",defaultValue = "5") Integer row,
                        HttpServletRequest request,Equipment equipment,FeedBack feedBack,
                        Model model){
        equipment.setStart(row*(page-1));
        equipment.setRows(row);
        Page<Equipment> page1 = equipmentService.selectAll1(equipment);
        page1.setPage(page);
        HttpSession session = request.getSession();
       model.addAttribute("page",page1);
        return "userequip";
    }

    @GetMapping("/userfeedback")
    public String listuf(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "row",defaultValue = "5") Integer row,
                        HttpServletRequest request,FeedBack feedBack,
                        Model model){
        HttpSession session = request.getSession();
        feedBack.setStart(row*(page-1));
        feedBack.setRows(row);
        feedBack.setUserId((Integer) session.getAttribute("userId"));
        Page<FeedBack> page2 = feedBackService.selectByUserId1(feedBack);
        page2.setPage(page);
        model.addAttribute("page",page2);
        return "userfeedback";
    }


    @GetMapping("/adminequip")
    public String lista(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "row",defaultValue = "5") Integer row,
                        Model model,
                        Equipment equipment){
        equipment.setStart(row*(page-1));
        equipment.setRows(row);
        Page<Equipment> page1 = equipmentService.selectAll1(equipment);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "adminequip";
    }

    @GetMapping("/adminmaintenance")
    public String listam(@RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "row",defaultValue = "5") Integer row,
                        Model model, MaintenanceRecord maintenanceRecord){
        maintenanceRecord.setStart(row*(page-1));
        maintenanceRecord.setRows(row);
        Page<MaintenanceRecord> page3 = maintenanceRecordService.selectAll1(maintenanceRecord);
        page3.setPage(page);
        model.addAttribute("page",page3);
        return "adminmaintenance";
    }

    @GetMapping("/adminfeedback")
    public String listam(@RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "row",defaultValue = "5") Integer row,
                         Model model,FeedBack feedBack){
        feedBack.setStart(row*(page-1));
        feedBack.setRows(row);
        Page<FeedBack> page2 = feedBackService.selectAll1(feedBack);
        page2.setPage(page);
        model.addAttribute("page",page2);
        return "adminfeedback";
    }

    @GetMapping("/equipmentselect")
    public String equipmentselect(Model model,@RequestParam("equipmentName") String equipmentName){
        Equipment equipment = new Equipment();
        equipment.setStart(0);
        equipment.setRows(3);
        equipment.setEquipmentName(equipmentName);
        Page<Equipment> page = equipmentService.selectByName(equipment);
        page.setPage(1);
        model.addAttribute("page",page);
        return "userequip";
    }

    @GetMapping("/equipmentselect1")
    public String equipmentselect1(Model model,@RequestParam("equipmentName") String equipmentName){
        Equipment equipment = new Equipment();
        equipment.setStart(0);
        equipment.setRows(5);
        equipment.setEquipmentName(equipmentName);
        Page<Equipment> page = equipmentService.selectByName(equipment);
        page.setPage(1);
        model.addAttribute("page",page);
        return "adminequip";
    }

    @DeleteMapping("/equipment/{id}")
    @ResponseBody
    public String deleteequipment(@PathVariable("id") Integer id){
        int i = equipmentService.deleteByPrimaryKey(id);
        System.out.println(i);
        return "OK";
    }

    @PostMapping("/equipment")
    @ResponseBody
    public String createequipment(Equipment equipment){
        int insert = equipmentService.insert(equipment);
        return "OK";
    }

    @DeleteMapping("/feedback/{id}")
    @ResponseBody
    public String deletefeedback(@PathVariable("id") Integer id){
        FeedBack feedBack = feedBackService.selectByPrimaryKey(id);
        if("未处理".equals(feedBack.getStatus())){
            return "";
        }
        int i = feedBackService.deleteByPrimaryKey(id);
        return "OK";
    }

    @DeleteMapping("/maintenance/{id}")
    @ResponseBody
    public String deletemaintenance(@PathVariable("id") Integer id){
        MaintenanceRecord maintenanceRecord = maintenanceRecordService.selectByPrimaryKey(id);
        if("未处理".equals(maintenanceRecord.getStatus())){
            return "";
        }
        int i = maintenanceRecordService.deleteByPrimaryKey(id);
        return "OK";
    }

    @PostMapping("/feedback")
    @ResponseBody
    public String createFeedBack(FeedBack feedBack,HttpServletRequest request,Model model){
        feedBack.setSubmissionTime(new Date());
        int insert = feedBackService.insert(feedBack);
        HttpSession session = request.getSession();
        feedBack.setStart(0);
        feedBack.setRows(5);
        feedBack.setUserId((Integer) session.getAttribute("userId"));
        Page<FeedBack> page2 = feedBackService.selectByUserId1(feedBack);
        page2.setPage(1);
        model.addAttribute("page",page2);
        return "OK";
    }
    @GetMapping("/maintenance")
    public String maintenance(HttpServletRequest request,Model model){
        MaintenanceRecord maintenanceRecord = maintenanceRecordService.selectByPrimaryKey(Integer.valueOf(request.getParameter("maintenanceId")));
        HttpSession session = request.getSession();
        String userName = userService.selectUserName((Integer) session.getAttribute("userId"));
        maintenanceRecord.setTechnicianName(userName);
        maintenanceRecord.setStatus("已处理");
        Date date = new Date();
        maintenanceRecord.setMaintenanceDate(date);
        int i1 = maintenanceRecordService.updateByPrimaryKey(maintenanceRecord);
        Equipment equipment = new Equipment();
        equipment.setEquipmentId(maintenanceRecord.getEquipmentId());
        equipment.setStatus("正常");
        int i = equipmentService.updateByPrimaryKey1(equipment);
        maintenanceRecord.setStart(0);
        maintenanceRecord.setRows(5);
        Page<MaintenanceRecord> page3 = maintenanceRecordService.selectAll1(maintenanceRecord);
        page3.setPage(1);
        model.addAttribute("page",page3);
        return "adminmaintenance";
    }

    @PostMapping("/maintenance")
    @ResponseBody
    public String createMaintenanceRecord(MaintenanceRecord maintenanceRecord,HttpServletRequest request){
        maintenanceRecord.setMaintenanceDate(new Date());
        maintenanceRecord.setTechnicianName("");
        int insert = maintenanceRecordService.insert(maintenanceRecord);
        Equipment equipment = new Equipment();
        equipment.setEquipmentId(maintenanceRecord.getEquipmentId());
        equipment.setStatus("维修中");
        equipmentService.updateByPrimaryKey1(equipment);
        List<Equipment> equipments = equipmentService.selectAll();
        HttpSession session = request.getSession();
        session.setAttribute("equipments",equipments);
        return "OK";
    }

    @GetMapping("/handlefeedback")
    public String handle(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        FeedBack feedBack = feedBackService.selectByPrimaryKey(Integer.valueOf(request.getParameter("feedBackId")));
        feedBack.setStatus("已处理");
        int i = feedBackService.updateByPrimaryKey(feedBack);
        feedBack.setStart(0);
        feedBack.setRows(5);
        Page<FeedBack> page2 = feedBackService.selectAll1(feedBack);
        page2.setPage(1);
        model.addAttribute("page",page2);
        return "adminfeedback";
    }

}
